@isTest
private class MeasureCalcFillControllerTest {
    static testMethod void testMethod1() {
        
           //产品结构
        ProductStructure__c ps1=new ProductStructure__c();
        ps1.Name='仪器';
        ps1.Classify1__c='生化';
        ps1.Classify2__c='生化-设备';
        ps1.Classify3__c='试剂';
        insert ps1;
         // 客户
        Account acc = TestUtils.createAccount();
        acc.Level__c = '一级医院';
        insert acc;

             // 客户
        Account acc1 = TestUtils.createAccount();
        acc1.Level__c = '一级医院';
        acc1.Name = 'xxxxxxxx';
        acc1.RecordTypeId = Utils.getRecordTypeIdByDevNameAndObjName('Maccura', 'Account');
        insert acc1;
        // 业务机会
        Opportunity opp = TestUtils.createOpportunity();
        opp.AccountId = acc.Id;
        opp.ExecutiveCompany__c = acc1.Id;
        insert opp;
        Id rt = Utils.getRecordTypeIdByDevNameAndObjName('NCProduct', 'Product__c');
        // 产品
        Product__c p = TestUtils.createProduct();
        p.bggsjhdj__c = 10.0;
        p.RecordTypeId = rt;
        p.Category0__c = '仪器';


        Product__c p2 = TestUtils.createProduct();
        p2.Name = 'aaaa';
        p2.bggsjhdj__c = 10.0;
        p2.RecordTypeId = rt;
        p2.Category0__c = '试剂';
        insert new Product__c[] {p, p2};

        // 意向产品
        IntentProduct__c ip = TestUtils.createIntentProduct();
        ip.Opportunity__c = opp.Id;
        ip.Product__c = p.Id;
        insert ip;

        // 价格
        Price__c price = TestUtils.createPrice();
        price.Product__c = p.Id;
        price.Company__c = '四川省迈克实业有限公司';
        price.InstCost__c = 10.0;
        Price__c price2 = TestUtils.createPrice();
        price2.Product__c = p2.Id;
        price2.Company__c = '四川省迈克实业有限公司';
        price2.InstCost__c = 10.0;
        insert new Price__c[] {price, price2};
        
            /// 日期处理
        Integer m = Date.today().month(); //当前月份
        Integer y = Date.today().year();  //当前年份 
       ///取上月
         if(m == 1){
        m = 12 ;
            y -= 1;
        }else{
        m -= 1;
            
        }
        
        String  month = String.valueOf(m) + '月';
        String  year= String.valueOf(y) + '年';
        AveragePrice__c av1 = new AveragePrice__c(
        Product__c = p.Id,
            Company__c = '四川省迈克实业有限公司',
            AveragePrice__c = 1.0,
            Year__c = year,
            Month__c = month,
             PriceProvince__c= '四川省/西藏'


            
        );
          AveragePrice__c av2 = new AveragePrice__c(
       		 Product__c = p2.Id,
            Company__c = '四川省迈克实业有限公司',
            AveragePrice__c = 1.0,
            Year__c = year,
            Month__c = month,
           PriceProvince__c= '四川省/西藏'

            
        );
        insert new AveragePrice__c[] {av2,av1};


        PageReference page = new PageReference('/apex/MeasureCalcFill?mid=' + opp.Id );
        System.Test.setCurrentPage(page);
        MeasureCalcFillController qr = new MeasureCalcFillController();

        qr.init();
        qr.oppId= opp.Id;
        qr.init();
        qr.getEquipmentFromIntentProduct();
        qr.searchJson = EncodingUtil.urlEncode('{"type":"FromProduct","brand":"","spec":"","cate0":"","cate1":"","nccode":"","ptype":"","serialNo":""}', 'UTF-8') ;
   		
        qr.search();

        List<MeasureCalcFillController.Measure_Calculate_ProductBean> resList = new List<MeasureCalcFillController.Measure_Calculate_ProductBean>(); 

        MeasureCalcFillController.Measure_Calculate_ProductBean ajst = new MeasureCalcFillController.Measure_Calculate_ProductBean(p2,price2,av2,new PriceList__c(),'MKSY',false,false);
        ajst.adprice =9.0;
        resList.add(ajst);


       qr.regentJson = EncodingUtil.urlEncode(JSON.serialize(resList), 'UTF-8');


        qr.resJson = EncodingUtil.urlEncode(JSON.serialize(resList), 'UTF-8');
        qr.adServiceJson = EncodingUtil.urlEncode('[{"name":"效果图制作","cost":"100"}]', 'UTF-8');
        qr.calcJson = EncodingUtil.urlEncode('{"subProfit":1,"subSale":1,"subIncome":1,"groupProfit":1,"groupSale":1,"groupIncome":1}', 'UTF-8');
        qr.save();

          List<Measure_Calculate__c> q = [select Id,Status__c from Measure_Calculate__c where Opportunity__c = :opp.Id];
        System.assertEquals(1, q.size());





          // ---------------------编辑-----------------------
        page = new PageReference('/apex/MeasureCalcFill?mid=' + q[0].Id);
        System.Test.setCurrentPage(page);
        qr = new MeasureCalcFillController();

        qr.init();
        qr.getDetails();
        //--------------------------------------------
        MeasureCalcStandardController mst = new MeasureCalcStandardController();
        mst.mcId = q[0].Id;
        mst.setNil();
        mst.init();
                System.Test.startTest();

        mst.getDetails();

         MeasureCalcStandardController mcsc = new MeasureCalcStandardController();
        mcsc.mcId = q[0].Id;
        mcsc.init();
        mcsc.setNil();
        mcsc.getDetails();


        PageReference page2 = new PageReference('/apex/MeasureCalcFillSimple?id=' + opp.Id);
        System.Test.setCurrentPage(page2);
        MeasureCalcFillSimpleController mss1  = new MeasureCalcFillSimpleController(new ApexPages.StandardController(new Measure_Calculate__c()));
        mss1.init();

        PageReference page1 = new PageReference('/apex/MeasureCalcFillSimple?id=' + q[0].Id);
        System.Test.setCurrentPage(page1);
        MeasureCalcFillSimpleController mss  = new MeasureCalcFillSimpleController(new ApexPages.StandardController(new Measure_Calculate__c()));
        mss.init();
        mss.setNil();
        mss.oppId = opp.Id ; 
        mss.init();

        List<MeasureCalcFillSimpleController.Measure_Calculate_ProductBean> tmp = new List<MeasureCalcFillSimpleController.Measure_Calculate_ProductBean>();
        MeasureCalcFillSimpleController.Measure_Calculate_ProductBean mcpb = new MeasureCalcFillSimpleController.Measure_Calculate_ProductBean();
        mcpb.cate1 ='生化';
        mcpb.cate2 = '试剂';
        mcpb.brand ='迈克';
        mcpb.estimatedSales = 10;
        mcpb.subgross = 10;
        mcpb.proincome = 10 ;
        tmp.add(mcpb);
        Map<String,Object> resMap = new Map<String,Object>();
        resMap.put('data', tmp);
        Map<String,Decimal> tmpMap = new Map<String,Decimal>();
        tmpMap.put('subProfit',1);
        tmpMap.put('subSale',1);
        tmpMap.put('subIncome',1);
        tmpMap.put('groupProfit',1);
        tmpMap.put('groupSale',1);
        tmpMap.put('groupIncome',1);
        resMap.put('calc', tmpMap );
        mss.saveJson = EncodingUtil.urlEncode(JSON.serialize(resMap), 'UTF-8');
        mss.save1();
        mss.mcId = q[0].Id;
        mss.init();
        mss.getDetails();
        System.debug('MeasureCalcFillSimple==>'+JSON.deserializeUntyped(EncodingUtil.urlDecode(mss.paramJson, 'UTF-8')));







    }
}